package com.adtech.farmProducts.activities.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.adtech.common.Common;
import com.adtech.common.http.AsyncHttpPostFormData;
import com.adtech.common.http.AsyncHttpResponse;
import com.adtech.common.http.AsyncOkHttpClient;
import com.adtech.common.http.AsyncResponseHandler;
import com.adtech.common.model.RspModel;
import com.adtech.common.utils.AccountManagers;
import com.adtech.common.widgets.load.LoadFactory;
import com.adtech.common.widgets.load.core.LoadManager;
import com.adtech.common.widgets.load.view.AbsView;
import com.adtech.common.widgets.recycler.RecyclerViewAdapter;
import com.adtech.common.widgets.recycler.decoration.DividerItemDecoration;
import com.adtech.farmProducts.R;
import com.adtech.farmProducts.activities.activity.InsertInfoActivity;
import com.adtech.farmProducts.adapter.InsertListAdapter;
import com.adtech.farmProducts.base.BaseFragment;
import com.adtech.farmProducts.model.FUserInfo;
import com.adtech.farmProducts.model.SourceInfo;
import com.adtech.farmProducts.widget.EmptyView;
import com.adtech.farmProducts.widget.ErrorView;
import com.adtech.farmProducts.widget.LoadingView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * Time:2020/6/20
 * Created by xiaocz
 * Describe 验货记录
 */
public class VerifyListFragment extends BaseFragment {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;


    /**
     * DATA
     */
    private LoadManager mLoadManager;
    private InsertListAdapter insertListAdapter;

    @Override
    protected int getLayoutSourceId() {
        return R.layout.fragment_list_insert;
    }

    @Override
    protected void initWidgets(View rootView) {
        super.initWidgets(rootView);
        mLoadManager = LoadFactory.getInstance().register(mRecyclerView, new AbsView.OnReloadListener() {
            @Override
            public void onReload(View view) {
                mLoadManager.showStateView(LoadingView.class);
                webList();
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if(isVisibleToUser){
            webList();
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    protected void initData() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext,
                DividerItemDecoration.VERTICAL, R.drawable.shape_divider_line, 8, false));
        insertListAdapter = new InsertListAdapter(new RecyclerViewAdapter.AdapterListenerImpl<SourceInfo>() {
            @Override
            public void onItemClick(RecyclerViewAdapter.ViewHolder<SourceInfo> holder, SourceInfo data) {
                super.onItemClick(holder, data);
                InsertInfoActivity.show(mContext,data);
            }
        });
        mRecyclerView.setAdapter(insertListAdapter);
        super.initData();
    }

    private void webList() {
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        HashMap<String, String> data = new HashMap<>();
        data.put("handleType", positionValue);
        data.put("sourceCode", "");
        data.put("companyId",new Gson().fromJson(AccountManagers.getAccount(), FUserInfo.class).getInfo().getCompanyId());
        data.put("userId", AccountManagers.getUserId());
        asyncHttpPostFormData.addFormData("data", data);
        asyncHttpPostFormData.addFormData("pageIndex", 1);
        asyncHttpPostFormData.addFormData("PageSize", Common.Constant.pageSize);
        okHttpClient.posts(Common.Constant.API_IP + "handle/queryHistory", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                mLoadManager.showStateView(ErrorView.class);
                e.printStackTrace();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                Type type = new TypeToken<RspModel<List<SourceInfo>>>() {
                }.getType();
                RspModel<List<SourceInfo>> rspModel = getAnalysisBase(mLoadManager, response, type, positionValue+"录入历史");
                if (rspModel == null) {
                    mLoadManager.showStateView(EmptyView.class);
                    return;
                }
                switch (rspModel.getCode()) {
                    case 1:
                        if (rspModel.getData() != null && !"[]".equals(rspModel.getData().toString())) {
                            insertListAdapter.replace(rspModel.getData());
                        }else {
                            mLoadManager.showStateView(EmptyView.class);
                        }
                        break;
                    default:
                        mLoadManager.showStateView(ErrorView.class);
                        break;
                }
            }
        });
    }

    private static String positionValue;

    /**
     * 显示我的碎片
     *
     * @return MineFragment
     */
    public static VerifyListFragment newInstance(String position) {
        positionValue=position;
        return new VerifyListFragment();
    }
}
